Practical Julia by Lee Phillips
Author:Lee Phillips
Language: eng
Format: epub
Publisher: No Starch Press
Published: 2024-11-15T00:00:00+00:00
Defining structs with Base.@kwdef
The default method for defining composite types leaves a bit to be desired. Its main deficiency is that the constructor it creates requires the programmer to remember the order in which a typeâs fields appear in its definition. The Base.@kwdef macro improves on this limitation by creating constructors that we can use with field names. For repeated use, itâs convenient to import this macro and rename it: import Base.@kwdef as @kwdef.
Letâs expand our geometry package with a new type representing ellipses as shown in Listing 8-5. This time weâll use @kwdef.
@kwdef struct Ellipse axis1::Real = 1 axis2::Real = 1 end
Listing 8-5: Defining an Ellipse type with @kwdef
This definition shows the second convenient feature of @kwdef: we can supply default values for fields. We also have the option to define a mutable struct with @kwdef mutable struct.
Letâs make an ellipse and assign it to a variable:
julia> oval = Ellipse(axis2=2.6) Ellipse(1, 2.6) julia> oval.axis1, oval.axis2 (1, 2.6)
This example shows how we can supply a subset of the typeâs keyword arguments, and the ones we omit will get their default arguments. As with functions, any keyword argument without a default in the type definition must be supplied when using the constructor. Also, similarly to functions, we may not mix positional and keyword forms:
julia> Ellipse(2, 3) Ellipse(2, 3) julia> Ellipse(2, axis2=3) ERROR: MethodError: no method matching Ellipse(::Int64; axis2=3)
As there is no drawback to using @kwdef when defining composite types, itâs convenient to use it routinely.
Because of the way Juliaâs JIT compiler works with the type system, computing with user-defined types is as fast as using native types. We can work at a higher level of abstraction, creating a set of types that naturally conform to the objects in our problem, without any compromise in performance.
Download
This site does not store any files on its server. We only index and link to content provided by other sites. Please contact the content providers to delete copyright contents if any and email us, we'll remove relevant links or contents immediately.
Practical Guide to Azure Cognitive Services by Chris Seferlis & Christopher Nellis & Andy Roberts(5949)
Unity Artificial Intelligence Programming - Fifth Edition by Dr. Davide Aversa(5548)
Serverless ETL and Analytics with AWS Glue by Vishal Pathak Subramanya Vajiraya Noritaka Sekiyama Tomohiro Tanaka Albert Quiroga Ishan Gaur(4698)
Open Source Projects - Beyond Code by John Mertic(3772)
The AI Product Manager's Handbook by Irene Bratsis(3745)
Graph Data Modeling in Python by Gary Hutson and Matt Jackson(3740)
Cloud Auditing Best Practices by Shinesa Cambric & Michael Ratemo(3364)
Aligning Security Operations with the MITRE ATT&CK Framework by Rebecca Blair(3339)
Graph Data Processing with Cypher by Anthapu Ravindranatha;(1538)
Data Literacy in Practice - A complete guide to data literacy and making smarter decisions with data through intelligent actions (2022) by Packt(1517)
Serverless Machine Learning with Amazon Redshift ML: Create, train, and deploy machine learning models using familiar SQL commands by Debu Panda Phil Bates Bhanu Pittampally Sumeet Joshi(1419)
Network Automation with Go by Nicolas Leiva & Michael Kashin(1368)
Data Literacy in Practice by Angelika Klidas Kevin Hanegan(1305)
Applied Machine Learning and High-Performance Computing on AWS by Mani Khanuja | Farooq Sabir | Shreyas Subramanian | Trenton Potgieter(1303)
Graph Data Processing with Cypher by Ravindranatha Anthapu(1292)
Unreal Engine 5 Game Development with C++ Scripting by Zhenyu George Li(1169)
Implementing Multifactor Authentication: Protect your applications from cyberattacks with the help of MFA by Marco Fanti(1163)
Fuzzing Against the Machine: Automate vulnerability research with emulated IoT devices on QEMU by Antonio Nappa Eduardo Blazquez(1157)
The AI Product Manager's Handbook: Develop a product that takes advantage of machine learning to solve AI problems by Irene Bratsis(1016)
